<div class="dialog-from">

  {#  <div class="from_alert">
        <h3>提示：</h3>
        <p>
            为了保障您的资金安全及权益，我们不允许主动修改您的姓名及出生日期等资料。如果您确实需要对以下内容进行修改，请提供必须的证明文件（如结婚证明，姓名更改通知或离婚判决书等），将证明发送至我们的客户服务邮箱service@middleforex.com，邮件标题请注明为：“账户个人资料修改+您的账户编号”，或者致电我们的客服热线：400-002-8180，我们的客服人员会协助您完成操作。</p>
    </div>#}

    <div class="form">
        <form id="form_bank_card" class="layui-form" action="">

            <div class="layui-form-item">
                <label class="layui-form-label">姓名:</label>
                <div class="layui-form-mid">{{ datas.basic.full_name }}</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">证件类型:</label>
                <div class="layui-form-mid">{{ identity_type[datas.basic.identity_type] }}</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">证件号码:</label>
                <div class="layui-form-mid">{{ datas.basic.identity_no }}</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>银行卡号:</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id" id="card_id"/>
                    <input type="text" id="card_card_no" name="card_no" lay-verify="card_no" autocomplete="off"
                           placeholder=""
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>银行国家:</label>
                <div class="layui-input-block">
                    <select name="country" lay-verify="country" lay-filter="country">
                        <option value="中国">中国</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>银行省:</label>
                <div class="layui-input-block">
                    <select id="card_province" name="province" lay-verify="province" lay-filter="province"  lay-search="" >
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>银行城市:</label>
                <div class="layui-input-block">
                    <select id="card_city" name="city" lay-verify="city" lay-filter="city"  lay-search="">
                        <option value="">请选择</option>

                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>银行名称:</label>
                <div class="layui-input-block">
                    <select id="card_bank_name" name="bank_name" lay-verify="bank_name"  lay-search="">
                        <option value="">请选择</option>
                        {% for bank in myselect.bank %}
                            <option value="{{ loop.key }}">{{ loop.key }}</option>
                        {% endfor %}
                        <option value="其他银行">其它</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>支行名称:</label>
                <div class="layui-input-block">
                    <input type="text" id="card_branch_name" name="branch_name" lay-verify="branch_name" autocomplete="off"
                           placeholder=""
                           class="layui-input" />
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>银行预留手机号:</label>
                <div class="layui-input-block">
                    <input type="text" id="card_bank_mobile" name="bank_mobile" lay-verify="bank_mobile" autocomplete="off"
                           placeholder=""
                           class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>验证码:</label>
                <div class="layui-input-block">
                    <input type="text" id="card_verify_code" style="width:50%;display:inline-block;" name="verify_code" lay-verify="verify_code" autocomplete="off"
                           placeholder=""
                           class="layui-input" />
                    <a href="javascript:void(0)" onclick="sendSMS(this);" style="display:inline-block;margin-bottom:2px; " title="获取验证码" class="layui-btn">获取验证码</a>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="formSubmit" onclick="return false;">保存</button>
                    <button class="layui-btn"  onclick="cancelSubmit();">取消</button>
                </div>
            </div>

        </form>
    </div>

</div>




<script type="text/javascript" src="/assets/scripts/area.js"></script>
<script>
    //初始数据
    var areaData = Area;
    var $form;
    var form;
    var $;

    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            $form = $('form');
        loadProvince();


        //加载省数据
        function loadProvince() {

            if (jQuery("#card_province option").length > 5) {
                return;
            }


            var proHtml = '';
            for (var i = 0; i < areaData.length; i++) {
                proHtml += '<option value="' + areaData[i].provinceName + '_' + areaData[i].mallCityList.length + '_' + i + '">' + areaData[i].provinceName + '</option>';
            }
            //初始化省数据
            $form.find('select[name=province]').append(proHtml);
            form.render();
            form.on('select(province)', function (data) {
                $form.find('select[name=area]').html('<option value="">请选择县/区</option>').parent().hide();
                var value = data.value;
                var d = value.split('_');
                var code = d[0];
                var count = d[1];
                var index = d[2];
                if (count > 0) {
                    loadCity(areaData[index].mallCityList);
                } else {
                    $form.find('select[name=city]').parent().hide();
                }
            });
        }

        //加载市数据
        function loadCity(citys) {
            var cityHtml = '';
            for (var i = 0; i < citys.length; i++) {
                cityHtml += '<option value="' + citys[i].cityName + '">' + citys[i].cityName + '</option>';
            }
            $form.find('select[name=city]').html(cityHtml).parent().show();
            form.render();
            form.on('select(city)', function (data) {
                var value = data.value;
                var d = value.split('_');
                var code = d[0];
                var count = d[1];
                var index = d[2];
            });
        }


        form.on('select(bank)', function (data) {
            var value = data.value;
            if (value == "其他银行") {
            }
        });


        //自定义验证规则
        form.verify({
            country: function (value) {
                if (value.length === 0 || (/^\s+$/.test(value))) {
                    return '请选择银行国家';
                }
            },
            province: function (value) {
                if (value.length === 0 || (/^\s+$/.test(value))) {
                    return '请选择银行省';
                }
            },
            city: function (value) {
                if (value.length === 0 || (/^\s+$/.test(value))) {
                    return '请选择银行市';
                }
            },
            bank_name: function (value) {
                if (value.length === 0 || (/^\s+$/.test(value))) {
                    return '请选择银行名称';
                }
            },
            branch_name: function (value) {
                if (value.length === 0 || (/^\s+$/.test(value))) {
                    return '请填写支行名称';
                }
            },
            card_no: function (value) {
                if (value.length === 0 || (/^\s+$/.test(value))) {
                    return '请填写银行卡号';
                }
                if (!(/^([1-9]{1})(\d{15}|\d{18})$/.test(value))) {
                    return '请填写正确的银行卡号';
                }

            },
            path: function (value) {
                if (value.length === 0) {
                    return '请上传银行卡图片';
                }
            }

        });

        //监听提交
        form.on('submit(formSubmit)', function (data) {
            data.field.province = data.field.province.split('_')[0];

            var url = (data.field.id && data.field.id.length > 0) ? "/myAccount/bankcard/update" : "/myAccount/bankcard/add";

            jQuery.ajax({
                url: url,
                type: "POST",
                dataType: "JSON",
                data: JSON.stringify(data.field),
                async: true,
                error: function (xData, status) {
                    layer.msg(xData.msg, {
                        icon: 2
                    });
                    return false;
                },
                success: function (xData, status) {
                    if (xData.status === 200) {
                        var card = data.field;
                        if (!card.id || card.id.length === 0) {
                            card.id = JSON.parse(xData.datas).id;
                        }

                        modifyCardList(card);
                        layer.msg("操作成功", {
                            icon: 1
                        });
                        jQuery(".layui-layer-ico").click();
                        emptyDialogData();
                    }
                    else {
                        layer.msg(xData.msg, {
                            icon: 0
                        });
                    }
                    return false;
                },
                contentType: "application/json; charset=\"utf-8\""
            });

            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // });
            return false;

        });

    });

    function cancelSubmit() {
        jQuery(".layui-layer-ico").click();
        emptyDialogData();
    }


    function initDialogData(data) {
        if (data && data.id.length > 0) {
            jQuery("#img_upload").attr("src", "/assets/member/images/pic.png");
            jQuery("#card_id").val(data.id);

            var province = jQuery("#card_province option[value*='" + data.province + "']").val();
            jQuery("#card_branch_name").val(data.branch_name);
            jQuery("#card_card_no").val(data.card_no);

            jQuery("#card_province").val(province);
            jQuery("dd[lay-value='" + province + "']").click();

            jQuery("#card_city").val(data.city);
            jQuery("dd[lay-value='" + data.city + "']").click();

            jQuery("#card_bank_name").val(data.bank_name);
            jQuery("dd[lay-value='" + data.bank_name + "']").click();

            jQuery("#card_bank_mobile").val(data.bank_mobile);
        }
        else {
            emptyDialogData();
        }
    }

    function emptyDialogData() {

        jQuery("#card_id").val("");
        jQuery("#card_branch_name").val("");
        jQuery("#card_card_no").val("");

        jQuery("#card_bank_mobile").val("");
        jQuery("#card_verify_code").val("");

        jQuery("#form_bank_card .layui-btn-disabled").html(jQuery("#form_bank_card .layui-btn-disabled").attr("title"));
    }

    //倒计时
    function countdown(obj, cnt){
        cnt--;
        var msg = obj.attr("title");
        if (cnt > 0 && (obj.attr("title") != obj.html())) {
            obj.html(msg + "(" + cnt + ")");
            var timer = setTimeout(function(){countdown(obj, cnt);}, 1000);
            console.log(timer);
        }
        else {
            obj.html(msg).removeClass("layui-btn-disabled");
        }
    }
    //发送验证码
    function sendSMS(o) {
        var obj = jQuery(o);
        if(obj.attr("title") != obj.html())
        {
            return;
        }

        var mobile = $("#card_bank_mobile").val();

        if(!isPhone(mobile))
        {
            layer.msg("请输入正确的手机号", {icon: 2, time: 1000,});
            return;
        }

        obj.html(obj.attr("title") + "...");
        $.ajax({
            url: "/common/send_sms",
            async: true,
            data: {mobile: mobile},
            type: "post",
            dataType: "json",
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            success: function (data) {
                if (data.status == 200) {
                    layer.msg("短信发送成功，请注意查收", {icon: 1, time: 1000,});
                    obj.addClass("layui-btn-disabled");
                    countdown(obj, 60);//倒计时60秒
                }
                else
                {
                    layer.msg(data.msg, {icon: 2, time: 1000,});
                    obj.html(obj.attr("title"));
                }
            },
            error: function (data) {
                layer.msg(data.msg, {icon: 2, time: 1000,});
                obj.html(obj.attr("title"));
            }
        });
    }

</script>